﻿mainModule.directive("ngDownloadButton", function () {
    return {
        restrict: "E",
        template: "<button></button>",
        replace : true,
        transclude: true,
        scope: {
            url: "@",
            method: "@",
            jsondata: "="
        },
        link:function(scope,element,attrs,ctrl, transclude)
        {
            var jsonData = scope.jsondata;
            var method = scope.method, url = scope.url;
            var inputs = '';

            for (name in jsonData) {
                inputs += '<input type=\'hidden\' name=\'' + name + '\' value=\'' + jsonData[name] + '\' />';
            };            

            element.append(transclude());
            element.on("click", function () {
                //send request
                $('<form action="' + url + '" method="' + (method || 'post') + '">' + inputs + '</form>')
                .appendTo('body').submit().remove();
            });
        }
    };
});